! function(win) {

	// 保存控件
	var list_arr = [];

	var FormCombox = function() {};

	// 添加所需控件
	FormCombox.prototype.addCombox = function(arrs) {
		if(arrs.length <= 0) return;
		for(var i in arrs) {
			if(typeof arrs[i].type == 'string') {
				// 开始创建控件
				createCombox(arrs[i]);
			} else {
				continue;
			}
		}
	};

	// 开始渲染
	FormCombox.prototype.reader = function(id) {
		if(id) {
			var elem = document.getElementById(id);
			if(elem) {
				for(var i in list_arr) {
					elem.appendChild(list_arr[i]);
				}
				list_arr = [];
			}
		} else {
			var elem = tools.create('div');
			elem.style.display = 'none';
			for(var i in list_arr) {
				elem.appendChild(list_arr[i]);
			}
			list_arr = [];
			var html = elem.innerHTML;
			elem = null;
			return html;
		}
	};

	// 检测并替换控件
	FormCombox.prototype.autoInit = function(id) {
		var elem = document.getElementById(id);
		if(elem) {
			var arrs = autoInitEach(elem);
			if(arrs.length >= 1) {
				for(var i in arrs) {
					autoInitCreate(arrs[i]);
				}
			}
		}
	};

	// 检测所有表单元素
	function autoInitEach(elem) {
		var list = [];
		var nodes = elem.getElementsByTagName('*');
		for(var i = 0; i < nodes.length; i++) {
			if(!nodes[i].tagName) continue;
			var tagName = nodes[i].tagName.toLowerCase();
			if(tagName == 'input' || tagName == 'textarea') {
				list.push(nodes[i]);
			}
		}
		return list;
	}

	// 开始替换元素
	function autoInitCreate(elem) {
		var opts = testing(elem),
			parent = elem.parentNode;

		var _input = tools.create('input');
		_input.type = 'hidden';
		opts.name && (_input.name = opts.name);
		opts.value && (_input.value = opts.value);

		if(opts.type == 'file') {
			var _node = creates.fileCombox(opts);
		}

		parent.insertBefore(_node, elem.nextSibling);
		parent.insertBefore(_input, elem.nextSibling);
		parent.removeChild(elem);
	}

	// 检测字段
	function testing(elem) {
		var opts = {
			'data': {},
			'param': {}
		};
		for(var i = 0; i < elem.attributes.length; i++) {
			if(/^data-/.test(elem.attributes[i].name)) {
				opts['data'][elem.attributes[i].name] = elem.attributes[i].value;
			} else if(/^param-/.test(elem.attributes[i].name)) {
				var _key = (elem.attributes[i].name).split('-')[1];
				opts['param'][_key] = elem.attributes[i].value;
			} else {
				opts[elem.attributes[i].name] = elem.attributes[i].value;
			}
		}
		return opts;
	}

	// 保存控件到数组
	function pusharr(opt, sort) {
		if(sort) {
			if(list_arr[sort]) {
				list_arr.splice(sort, 0, opt);
			} else {
				list_arr[sort] = opt;
			}
		} else {
			list_arr.push(opt);
		}
	}

	// 创建控件
	function createCombox(opts) {
		if(opts.type == 'hidden') {
			var input = tools.create('input');
			input.type = 'hidden';
			input.name = opts.name;
			input.value = opts.value;
			pusharr(input, opts.sort);
		} else {
			var _div = tools.create('div');
			_div.className = 'layout-combox ' + (typeof opts.class == 'string' ? opts.class : '');
			var label = tools.create('label');
			label.className = 'layout-form-label';
			label.innerText = opts.label;
			var _ipt = tools.create('div');
			_ipt.className = 'layout-form-ipt';
			if(opts.type == 'text' || opts.type == 'password' || opts.type == 'textarea') {
				var _com;
				if(opts.type == 'textarea') {
					_com = creates.tearaCombox(opts);
				} else if(opts.type == 'text') {
					_com = creates.textCombox(opts);
				} else if(opts.type == 'password') {
					_com = creates.passCombox(opts);
				}
				_ipt.appendChild(_com);
			} else if(opts.type == 'radio') {
				if(opts.radio.length >= 1) {
					var div = tools.create('div');
					div.className = 'layout-form-radio';
					typeof opts.disabled != 'undefined' && opts.disabled != false ? div.setAttribute('disabled', 'disabled') : null;
					var input = tools.create('input');
					input.type = 'hidden';
					input.name = opts.name;
					div.appendChild(input);
					var radio = opts.radio;
					for(var i in radio) {
						div.appendChild(creates.radioCombox(opts, radio[i], input));
					}
					_ipt.appendChild(div);
				}
			} else if(opts.type == 'checkbox') {
				if(opts.checkbox.length >= 1) {
					var div = tools.create('div');
					div.className = 'layout-form-checkbox';
					typeof opts.disabled != 'undefined' && opts.disabled != false ? div.setAttribute('disabled', 'disabled') : null;
					var input = tools.create('input');
					input.type = 'hidden';
					input.name = opts.name;
					input.value = (opts.value || '');
					div.appendChild(input);
					var checkbox = opts.checkbox;
					for(var i in checkbox) {
						div.appendChild(creates.checkboxCombox(opts, checkbox[i], input));
					}
					_ipt.appendChild(div);
				}
			} else if(opts.type == 'select') {
				_ipt.appendChild(creates.selectCombox(opts));
			} else if(opts.type == 'switch') {
				_ipt.appendChild(creates.switchCombox(opts));
			} else if(opts.type == 'custom') {
				_ipt.innerHTML = opts.html;
			}
			_div.appendChild(label);
			_div.appendChild(_ipt);
			pusharr(_div, opts.sort);
		}
	}

	// 创建某个控件
	var creates = {
		'textCombox': function(opts) {
			var _input = tools.create('input');
			_input.type = 'text';
			opts.name && (_input.name = opts.name);
			opts.value && (_input.value = opts.value);
			opts.disabled && (_input.disabled = opts.disabled);
			opts.placeholder && (_input.placeholder = opts.placeholder);
			return _input;
		},
		'passCombox': function(opts) {
			var _input = tools.create('input');
			_input.type = 'password';
			opts.name && (_input.name = opts.name);
			opts.value && (_input.value = opts.value);
			opts.disabled && (_input.disabled = opts.disabled);
			opts.placeholder && (_input.placeholder = opts.placeholder);
			return _input;
		},
		'tearaCombox': function(opts) {
			var _textarea = tools.create('textarea');
			opts.name && (_textarea.name = opts.name);
			opts.value && (_textarea.value = opts.value);
			opts.disabled && (_textarea.disabled = opts.disabled);
			opts.placeholder && (_textarea.placeholder = opts.placeholder);
			return _textarea;
		},
		'radioCombox': function(opts, _radio, input) {
			var span = tools.create('span');
			var a = tools.create('a');
			a.href = 'javascript:;';
			a.setAttribute('data-value', _radio['value']);
			if(typeof opts.disabled == 'undefined' && opts.disabled != false) {
				a.onclick = function(e) {
					tools.stopPropagation(e);
					var jude = this.getAttribute('checked');
					if(!jude) {
						var sibling = this.parentNode.parentNode.getElementsByTagName('a');
						for(var _ain = 0; _ain < sibling.length; _ain++) {
							sibling[_ain].removeAttribute('checked');
							sibling[_ain].childNodes[0].className = 'icon-unselected';
						}
						this.setAttribute('checked', 'checked');
						this.childNodes[0].className = 'icon-selected';
						this.parentNode.parentNode.childNodes[0].value = this.getAttribute('data-value');
					}
				}
			}
			var _i = tools.create('i');
			if(typeof _radio['checked'] != 'undefined' && _radio['checked'] != false) {
				a.setAttribute('checked', 'checked');
				_i.className = 'icon-selected';
				input.value = _radio['value'];
			} else {
				_i.className = 'icon-unselected';
			}
			var font = tools.create('font');
			font.innerText = _radio['text'];
			a.appendChild(_i);
			span.appendChild(a);
			span.appendChild(font);
			return span;
		},
		'checkboxCombox': function(opts, _checkbox, input) {
			var _ot, _ov;
			if(typeof _checkbox == 'object') {
				_ot = _checkbox['text'];
				_ov = _checkbox['value'];
			} else if(typeof _checkbox == 'string' || typeof _checkbox == 'number' || typeof _checkbox == 'boolean') {
				_ot = _ov = _checkbox;
			} else {
				_ot = _ov = '';
			}
			var span = tools.create('span');
			var a = tools.create('a');
			a.href = 'javascript:;';
			a.setAttribute('data-value', _ov);
			if(typeof opts.disabled == 'undefined' && opts.disabled != false) {
				a.onclick = function(e) {
					tools.stopPropagation(e);
					var jude = this.getAttribute('checked'),
						value = this.parentNode.parentNode.childNodes[0].value,
						_val = this.getAttribute('data-value');
					if(jude) {
						var _len = value.length;
						value = value.replace(',' + _val, '');
						value = value.replace(_val + ',', '');
						(_len == value.length) && (value = '');
						this.removeAttribute('checked', 'checked');
						this.childNodes[0].className = 'icon-checkbox';
					} else {
						if (value != '') value = value + ',' + _val;
						else value = _val;
						this.setAttribute('checked', 'checked');
						this.childNodes[0].className = 'icon-checkboxed';
					}
					this.parentNode.parentNode.childNodes[0].value = value;
				}
			}
			var _i = tools.create('i'),
				_value = input.value;
			if(typeof _checkbox['checked'] != 'undefined' && _checkbox['checked'] != false) {
				a.setAttribute('checked', 'checked');
				_i.className = 'icon-checkboxed';
				if (_value == '') {
					input.value = _ov;
				} else {
					var reg = new RegExp(_ov, 'gi');
					if (!reg.test(_value)) {
						input.value = _value + ',' + _ov;
					}
				}
			} else {
				if (!_value == '') {
					var reg = new RegExp(_ov, 'gi');
					if (reg.test(_value)) {
						a.setAttribute('checked', 'checked');
						_i.className = 'icon-checkboxed';
					} else {
						_i.className = 'icon-checkbox';
					}
				} else {
					_i.className = 'icon-checkbox';
				}
			}
			var font = tools.create('font');
			font.innerText = _ot;
			a.appendChild(_i);
			span.appendChild(a);
			span.appendChild(font);
			return span;
		},
		'selectCombox': function(opts) {
			var select = tools.create('div');
			select.className = 'layout-form-select';
			if(typeof opts.disabled == 'undefined' && opts.disabled != 'disabled') {
				select.onclick = function(e) {
					tools.stopPropagation(e);
					var _self = this,
						selected = _self.getAttribute('layout-selected'),
						_ul = _self.childNodes[1];
					if(selected == 'on') {
						_self.setAttribute('layout-selected', 'off');
						_ul.style.display = 'none';
					} else {
						_self.setAttribute('layout-selected', 'on');
						_ul.style.display = 'block';
					}
				}
			} else {
				select.setAttribute('disabled', 'disabled');
			}
			var _title = tools.create('div');
			_title.className = 'layout-select-title';
			var _input = tools.create('input');
			_input.type = 'hidden';
			_input.name = opts.name;
			(typeof opts.value == 'string') && (_input.value = opts.value);
			var _p = tools.create('p');
			(typeof opts.text == 'string') && (_p.innerHTML = opts.text);
			var ul = tools.create('ul');
			ul.className = 'layout-select-container';
			var option = opts.option;
			if(option.length >= 1) {
				for(var i in option) {
					var _ot, _ov;
					if(typeof option[i] == 'object') {
						_ot = option[i]['text'];
						_ov = option[i]['value'];
					} else if(typeof option[i] == 'string' || typeof option[i] == 'number' || typeof option[i] == 'boolean') {
						_ot = _ov = option[i];
					} else {
						_ot = _ov = '';
					}
					var li = tools.create('li');
					li.setAttribute('data-value', _ov);
					li.innerHTML = _ot;
					li.onclick = function(e) {
						tools.stopPropagation(e);
						var _self = this,
							parent = _self.parentNode,
							prev = parent.previousSibling,
							_input = prev.childNodes[0],
							_p = prev.childNodes[1];
						_input.value = _self.getAttribute('data-value');
						_p.innerHTML = _self.innerText;
						parent.parentNode.click();
					}
					ul.appendChild(li);
					if(typeof option[i]['selected'] != 'undefined' && option[i]['selected'] != false) {
						_p.innerHTML = _ot;
						_input.value = _ov;
					}
				}
			}
			_title.appendChild(_input);
			_title.appendChild(_p);
			select.appendChild(_title);
			select.appendChild(ul);
			return select;
		},
		'switchCombox': function(opts) {
			var _switch = tools.create('div');
			_switch.className = 'layout-form-switch';
			var data_off = opts.switch[0],
				data_on = opts.switch[1];
			var a = tools.create('a');
			a.href = 'javascript:;';
			a.className = 'off';
			var em = tools.create('em');
			var font = tools.create('font');
			font.innerHTML = data_off.text;
			var _input = tools.create('input');
			_input.type = 'hidden';
			_input.name = opts.name;
			_input.value = data_off.value;
			if(typeof data_on.checked != 'undefined' && data_on.checked != false) {
				_input.value = data_on.value;
				font.innerHTML = data_on.text;
				a.setAttribute('checked', 'checked');
				a.className = 'on';
			}
			a.onclick = function(e) {
				tools.stopPropagation(e);
				var isChecked = this.getAttribute('checked');
				if(isChecked) {
					this.removeAttribute('checked');
					this.className = 'off';
					_input.value = data_off.value;
					font.innerHTML = data_off.text;
					data_off.callback && data_off.callback('off');
				} else {
					this.setAttribute('checked', 'checked');
					this.className = 'on';
					_input.value = data_on.value;
					font.innerHTML = data_on.text;
					data_on.callback && data_on.callback('on');
				}
			}
			a.appendChild(em);
			a.appendChild(font);
			_switch.appendChild(_input);
			_switch.appendChild(a);
			return _switch;
		},
		'fileCombox': function(opts) {
			var _upload_form = tools.create('form');
			_upload_form.style.width = '0px';
			_upload_form.style.height = '0px';
			_upload_form.style.overflow = 'hidden';
			_upload_form.action = opts.action;
			_upload_form.enctype = 'multipart/form-data';
			_upload_form.method = 'post';
			if(typeof opts.param == 'object') {
				for(var i in opts.param) {
					if(i == 'file') continue;
					var param = opts.param[i];
					var _h = tools.create('input');
					_h.type = 'hidden';
					_h.name = i;
					_h.value = param;
					_upload_form.appendChild(_h);
				}
			}
			var _file = tools.create('input');
			_file.type = 'file';
			_file.name = opts.param.file || 'file';
			_file.onchange = function(e) {
				tools.stopPropagation(e);
				_upload_form.onsubmit();
			};
			_upload_form.appendChild(_file);
			document.body.appendChild(_upload_form);

			var _upload = tools.create('div');
			_upload.className = 'layout-form-upload';
			var _a = tools.create('a');
			_a.href = 'javascript:;';
			_a.onclick = btn_a;
			var _i = tools.create('i');
			_i.className = 'icon-upload';
			var _font = tools.create('font');
			_font.innerText = opts.text;
			var _status = tools.create('span');
			_status.className = 'layout-upload-status';
			var _s_i = tools.create('i');
			_status.appendChild(_s_i);
			_a.appendChild(_i);
			_a.appendChild(_font);
			_upload.appendChild(_a);
			_upload.appendChild(_status);

			var upload_callback = opts['data']['data-event'] ? eval(opts['data']['data-event']) : {};
			if(typeof upload_callback.before == 'undefined') {
				upload_callback.before = function() {
					_s_i.className = 'icon-loading';
				};
			}
			if(typeof upload_callback.complete == 'undefined') {
				upload_callback.complete = function() {

				};
			}
			_upload_form.onsubmit = function(e) {
				tools.preventDefault(e);
				_a.onclick = null;
				upload_callback.before();
				// 利用 ajaxForm 上传文件
				$(_upload_form).ajaxSubmit({
					complete: function(xhr) {
						console.dir(xhr);
						_a.onclick = btn_a;
						if(xhr.status >= 200 && xhr.status <= 302) {
							_s_i.className = 'icon-ok';
						} else {
							_s_i.className = 'icon-fail';
						}
						upload_callback.complete(xhr);
					},
					resetForm: false,
					dataType: 'json'
				});
				_upload_form.reset();
				return false;
			};

			function btn_a(e) {
				tools.stopPropagation(e);
				_file.click();
			}
			return _upload;
		}
	}

	// 工具
	var tools = {
		create: function(name) {
			return document.createElement(name);
		},
		stopPropagation: function(e) {
			var e = e || window.event;
			if(e.stopPropagation) {
				e.stopPropagation();
			} else {
				e.cancelBubble = true;
			}
		},
		preventDefault: function(e) {
			var e = e || window.event;
			if(e.preventDefault) {
				e.preventDefault();
			} else {
				e.returnValue = true;
			}
		}
	}

	win.FormCombox = FormCombox;
}(window);